<?php

/**
 * Class permettant de convertir un PDF en format JPG avec textes séparé pour indexation et recherches
 */


class php2flip {

	const debug = true;
	
    const geometry = '1024x1300';
    const density = '150x150';
    const resample = '150x150';
    const quality = '85';

    const folder_pdf_to_encode = 'sources/to_encode/';
    const folder_pdf_encoded = 'sources/encoded/';
    const folder_pic = 'media/flipbook/images/';
    const folder_txt = 'media/flipbook/text/';

    const ext_pdf = 'pdf';	// Extension d'un fichier pdf

    const prefix_pic = 'pic';	// Prefix des fichiers généré en jpg

    private $file;

    public function __construct(){
        $this->checkToEncode();
    }

    /*
     * Verifie le dossier d'upload pour effectuer une conversion auto
     */
    public function checkToEncode(){
        $files = $this->listFiles(self::folder_pdf_to_encode, self::ext_pdf);
        if(is_array($files) && count($files) > 0){
		
			$this->debug(count($files).' file(s) found');	
            $this->setFile($files[0]);
			
			/*
            $this->pdf2jpeg();
			$this->movingFile();
			*/
			
        }else {
			$this->debug('no file waiting');
		}
    }

    /*
     * Convertir le pdf en images jpeg
     * nécessite imagesmagick d'installé sur le server
     */
    public function pdf2jpeg($file = false){
		$this->debug('pdf2jpeg');
		
        if($file)
            $this->setFile($file);
			
        $source = self::folder_pdf_to_encode.$this->getFile();
        if(!$source){
            $this->debug('no file ?');
            return false;
        }else {
		
			$this->debug('file to encode '.$source);
		
			// Dossier d'upload du pdf
			$folderUp = time();
			
			// Création du dossier
			mkdir(self::folder_pic.$folderUp);
			$this->debug('create folder '.self::folder_pic.$folderUp);	
			
			// Commande de conversion
            $cmd = "convert -geometry ".self::geometry." -strip -density ".self::density." -resample ".self::resample." -quality ".self::quality." -units PixelsPerInch ".$source." -type TrueColor ".self::folder_pic.$folderUp.'/'.self::prefix_pic.".jpg";
            $this->debug($cmd);			
			
			// Exécution de la commande
			exec($cmd);
			$this->debug('convert ok');	
			
            return true;
        }
    }
	
    /*
     * Deplace les fichiers après conversion
     */
	private function movingFile(){
		if(rename(self::folder_pdf_to_encode.$this->getFile(), self::folder_pdf_encoded.$this->getFile()))
			$this->debug('file move in '.self::folder_pdf_encoded.$this->getFile());		
	}
	
	
    /*
     * Liste les fichiers
     */
    private function listFiles( $dir, $getExt = '')
    {
        if(!is_dir($dir))
           return false;
        elseif(substr($dir,-1) !== DIRECTORY_SEPARATOR)
           $dir .= DIRECTORY_SEPARATOR;
        
        if(!empty($getExt) && $getExt[0] !== '.')
            $getExt = '.'.$getExt;

        $ret = array();
        foreach(glob($dir.'*'.$getExt, GLOB_NOSORT) as $contents)
        {
            if(is_file($contents))
              $ret[] = str_replace($dir,'',$contents);
        }
        return $ret;
    }

    public function setFile($file){
        $this->file = $file;
    }
    public function getFile(){
        return $this->file;
    }
	
	private function debug($text){
		if(self::debug){
			echo $text."<br />";
		}
	}

}